<?php
/**
 * Created by PhpStorm.
 * User  : wubin
 * Tel   : 18535741189
 * Wechat: 290646986
 * Date  : 2017/6/14
 * Time  : 下午12:19
 */

namespace module;

use houdunwang\request\Request;
use system\model\Keywords;

trait Wechat
{
    //判断关键词
    final protected function saveKeywords($data)
    {
        $module = isset($data['module']) ? $data['module'] : Request::get('m');
        $where = [
            ['module_id',$data['module_id']],
            ['module',$module]
        ];
        $keyWordsModel = Keywords::where($where)->first() ? : new Keywords();
        $data['module'] = $module;
        $keyWordsModel->save($data);
    }
    /**
     * 分配变量关键词
     *  关键词表base_content module_id  ，  也就是对应baseContent表中主键id
     */
    final protected function assignKeywords($modeu_id)
    {
        //1.
        $where = [
            ['module_id', $modeu_id],
            ['module', Request::get('m')]
        ];
        /**
         *  module_id -- $modeu_id 结果： ↓       module --get参数m=base
         *  [0] => Array
         * (
         * [0] => module_id
         * [1] => 1
         * )
         *
         * [1] => Array
         * (
         * [0] => module
         * [1] => base
         * )
         */
        //p($where);
        //2.
        $keyWordsModel = Keywords::where($where)->first() ?: new Keywords();
        //p($keyWordsModel);
        /**
         *  [keywords] => Array
         * (
         * [field] => keywords
         * [type] => varchar(255)
         * [null] => NO
         * [key] =>
         * [default] =>
         * [extra] =>
         * )
         */
        //分配变量 keywords ，
        View::with('keywords', $keyWordsModel['keywords']);
    }

    /**
     * 删除关键词
     */
    final protected function delKeywords($modeu_id)
    {
        //1.
        $where = [
            ['module_id',$modeu_id],
            ['module',Request::get('m')]
        ];
        //2.
        $keyWordsModel = Keywords::where($where)->first() ? : new Keywords();
        //3.执行删除
        $keyWordsModel->destory();
    }

}